<?php

class rememberFilter extends sfFilter
{

    public function execute($filterChain)
    {
        // Execute this filter only once
        if ($this->isFirstCall())
        {
             $cookieName  = sfConfig::get('app_cookie_name');
            // Filters don't have direct access to the request and user objects.
            // You will need to use the context object to get them
            $request     = $this->getContext()->getRequest();
            $userSession = $this->getContext()->getUser();
            if (!$userSession->getAttribute('vidsUser', false))
            {
               $cookie = $request->getCookie($cookieName);
                if ($cookie)
                {
                    // sign in
                    $user = UserTable::getInstance()->findOneByRememberMeCode($cookie);
                    if ($user)
                    {
                        // authenticated user
                        $userSession->setAuthenticated(true);

                        /*
                         * Add credential
                         * by Mina.gerges
                         */

                        if ($user->getRole()->getType() == Role::CONTROLLER)
                        {
                            $userSession->addCredential('admin');
                            if (!$user->getRole()->getEditable())
                            {
                                $userSession->addCredential('superAdmin');
                            }
                        }
                        // create MaxUser object
                        $maxUserObject = new MaxUser($user);
                        $userSession->setAttribute('MaxUser', $maxUserObject);

                        // authenticated user
                        //remove from suspecious after true login
                        SuspiciousLoginTable::getInstance()->deleteAfterTrueLogin($request->getRemoteAddress());
                        // change last login
                        UserTable::getInstance()->changeLastLogin($user['id']);
                    }
                }
            }
        }
        // Execute next filter
        $filterChain->execute();
    }

}

?>
